home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Think Class Libraries / MacTCP class library / MacTCP class sources / CTCP.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-30  |  14.0 KB  |  589 lines  |  [TEXT/KAHL]

  1. /*
  2.  
  3.   CTCP.c
  4.   Superclass:    CMacTCPDriver
  5.  
  6.      The TCP implementation (Chapter 4, MacTCP Programmer's Guide).
  7.  
  8.   Copyright © NCSA, University of Illinois;    June 2, 1992
  9.   Eric Johnson, John Newlin and Igor Livshits
  10.   
  11.   This code may be used, modified, and distributed free of charge and obligation.
  12.   
  13. */
  14.  
  15.  
  16. #include    "CTCP.h"
  17.  
  18. /*=====================*/
  19. /*===---------------===*/
  20.  
  21. OSErr CTCP::ITCP(void)
  22. Begin
  23.     CMacTCPDriver::IMacTCPDriver();                        //    Initialize the driver
  24.                                                                                         //    Initialize all instance variables
  25.     remoteAddress=        Null;
  26.     localAddress=            Null;
  27.     destinationPort=    Null;
  28.     sourcePort=                Null;
  29.     *remoteHostName=    kEOL;
  30.     streamID=                    Null;
  31.     streamLength=            Null;
  32.     streamBuffer=            Null;
  33.     inBuffer=                    Null;
  34.     inDataLength=            Null;
  35.     outBuffer=                Null;
  36.     outDataLength=        Null;
  37.     timeOutDelay=            kTimeOut;
  38.     fMark=                        False;
  39.     fUrgent=                    False;
  40.     itsLastError=            noErr;
  41. End
  42.  
  43. /*===---------------===*/
  44.  
  45. void CTCP::Dispose(void)
  46. Begin
  47.     Release();
  48.     
  49.     inherited::Dispose();
  50. End
  51.  
  52. /*===---------------===*/
  53.  
  54. void CTCP::SetTimeOut(byte timeOut)
  55. Begin
  56.     timeOutDelay= timeOut;                                        //    Time out delay
  57. End
  58.  
  59. /*===---------------===*/
  60.  
  61. byte CTCP::GetTimeOut(void)
  62. Begin
  63.     return timeOutDelay;                                            //    Current time out delay
  64. End
  65.  
  66. /*===---------------===*/
  67.  
  68. void CTCP::SetDestination(char* name, ip_addr address, ip_port port)
  69. Begin
  70.     MoveHHi((Handle)this);
  71.     HLock((Handle)this);                                            //    Lock our object
  72.     
  73.     strncpy(remoteHostName, name, kNameLength);
  74.     remoteAddress= address;
  75.     destinationPort= port;
  76.     
  77.     HUnlock((Handle)this);
  78. End
  79.  
  80. /*===---------------===*/
  81.  
  82. void CTCP::GetDestination(char* name, ip_addr* address, ip_port* port)
  83. Begin
  84.     MoveHHi((Handle)this);
  85.     HLock((Handle)this);                                            //    Lock our object
  86.     
  87.     strncpy(name, remoteHostName, kNameLength);
  88.     *address= remoteAddress;
  89.     *port= destinationPort;
  90.     
  91.     HUnlock((Handle)this);
  92. End
  93.  
  94. /*===---------------===*/
  95.  
  96. void CTCP::Wait(void)
  97. Begin
  98.     return;
  99. End
  100.  
  101. /*===---------------===*/
  102.  
  103. void CTCP::MakeStreamBuffer(Size length)
  104. Begin
  105.     Ptr    buffer;
  106.  
  107.     streamLength= length;
  108.     
  109.     ForgetPtr(streamBuffer);
  110.     buffer= NewPtrClear(streamLength);
  111.     streamBuffer= buffer;
  112. End
  113.  
  114. /*===---------------===*/
  115.  
  116. void CTCP::PlaceData(Ptr data, Size length)
  117. Begin
  118.     Ptr    theData;
  119.  
  120.     ForgetPtr(outBuffer);                                            //    Release previous buffer
  121.     
  122.     TRY
  123.     {
  124.         theData= NewPtrClear(length);
  125.         outBuffer= theData;
  126.     
  127.         BlockMove(data, outBuffer, length);
  128.         outDataLength= length;
  129.     }
  130.     CATCH
  131.     {
  132.         ForgetPtr(outBuffer);
  133.         outDataLength= Null;
  134.     }
  135.     ENDTRY
  136. End
  137.  
  138. /*===---------------===*/
  139.  
  140. void CTCP::CreateInputBuffer(Ptr* data, Size length)
  141. Begin
  142.     Ptr    theData;
  143.  
  144.     TRY
  145.     {
  146.         theData= NewPtrClear(length);                    //    We need to allocate a new one
  147.         *data= inBuffer= theData;;                        //    Make sure the rest of the program can see it
  148.         inDataLength= length;
  149.     }
  150.     CATCH
  151.     {
  152.         ForgetPtr(inBuffer);
  153.         inDataLength= Null;
  154.     }
  155.     ENDTRY
  156. End
  157.  
  158. /*===---------------===*/
  159.  
  160. OSErr CTCP::SetInputBuffer(Ptr data, Size length)
  161. Begin
  162.     if (data)
  163.     {                                                                                //    Set to passed buffer
  164.         inBuffer= data;
  165.         inDataLength= length;
  166.         return noErr;
  167.     }
  168.     else
  169.         return kInvalidBufPtr;                                //    Null pointer
  170. End
  171.  
  172. /*===---------------===*/
  173.  
  174. Size CTCP::GetInputLength(void)
  175. Begin
  176.     return inDataLength;
  177. End
  178.  
  179. /*===--------------===*/
  180.  
  181. Ptr    CTCP::GetInputBuffer(void)
  182. Begin
  183.     return inBuffer;                                                   //    Assume dynamic allocation
  184. End
  185.  
  186. /*===--------------===*/
  187.  
  188. void    CTCP::ReleaseStreamBuffer(void)
  189. Begin
  190.     ForgetPtr(streamBuffer);                                   //    Release the buffer
  191.     streamLength= Null;                                                //    We have nothing!
  192. End
  193.  
  194. /*===--------------===*/
  195.  
  196. void    CTCP::ReleaseInputBuffer(void)
  197. Begin
  198.     ForgetPtr(inBuffer);                                           //    Release the buffer
  199.     inDataLength= Null;                                                //    We have nothing!
  200. End
  201.  
  202. /*===--------------===*/
  203.  
  204. void    CTCP::ReleaseOutputBuffer(void)
  205. Begin
  206.     ForgetPtr(outBuffer);                                           //    Release the buffer
  207.     outDataLength= Null;                                                //    We have nothing!
  208. End
  209.  
  210. /*===--------------===*/
  211.  
  212. void    CTCP::ReleaseBuffers(void)
  213. Begin
  214.     ReleaseOutputBuffer();
  215.     ReleaseInputBuffer();
  216.     ReleaseStreamBuffer();
  217. End
  218.  
  219. /*===--------------===*/
  220.  
  221. OSErr CTCP::GetLastError(void)
  222. Begin
  223.     return itsLastError;
  224. End
  225.  
  226. /*===---------------===*/
  227.  
  228. OSErr    CTCP::Create(void)
  229. Begin
  230.     TCPiopb            pb;                                                        //    Our parameters
  231.     TCPiopbPtr    parameters= &pb;
  232.     
  233.     GetParameters((ParmBlkPtr*)¶meters);
  234.         
  235.     parameters->csCode=        TCPCreate;
  236.     parameters->ioResult=    kInProgress;
  237.     
  238.     parameters->csParam.create.rcvBuff=         streamBuffer;
  239.     parameters->csParam.create.rcvBuffLen=     streamLength;
  240.     parameters->csParam.create.notifyProc=     Null;
  241.     
  242.     SetParameters((ParmBlkPtr)parameters);
  243.     SendControlInfoToDriver();                                //    Set our action...
  244.     Process();                                                                //    Let it run
  245.     
  246.     GetParameters((ParmBlkPtr*)¶meters);
  247.     streamID= parameters->tcpStream;
  248.     return itsLastError;
  249. End
  250.  
  251. /*===---------------===*/
  252.  
  253. OSErr CTCP::PassiveOpen(void)
  254. Begin
  255.     TCPiopb            pb;                                                        //    Our parameters
  256.     TCPiopbPtr    parameters= &pb;
  257.     
  258.     GetParameters((ParmBlkPtr*)¶meters);
  259.     
  260.     parameters->csCode=        TCPPassiveOpen;
  261.     parameters->ioResult=    kInProgress;
  262.     
  263.     parameters->csParam.open.ulpTimeoutValue=         timeOutDelay;
  264.     parameters->csParam.open.ulpTimeoutAction=        kDefaultAction;
  265.     parameters->csParam.open.validityFlags=                kDefaultFlags;
  266.     parameters->csParam.open.commandTimeoutValue= timeOutDelay;
  267.     parameters->csParam.open.remoteHost=                     Null;
  268.     parameters->csParam.open.remotePort=                    destinationPort;
  269.     parameters->csParam.open.localPort=                     sourcePort;
  270.     parameters->csParam.open.tosFlags=                        Null;
  271.     parameters->csParam.open.precedence=                     Null;
  272.     parameters->csParam.open.dontFrag=                         False;
  273.     parameters->csParam.open.timeToLive=                     Null;
  274.     parameters->csParam.open.security=                         Null;
  275.     parameters->csParam.open.optionCnt=                     Null;
  276.     
  277.     SetParameters((ParmBlkPtr)parameters);
  278.     SendControlInfoToDriver();                                //    Set our action...
  279.     if (!fAsynchronous)
  280.         Process();                                                            //    Let it run
  281.         
  282.     GetParameters((ParmBlkPtr*)¶meters);
  283.     remoteAddress=        parameters->csParam.open.remoteHost;
  284.     destinationPort=    parameters->csParam.open.remotePort;
  285.     localAddress=            parameters->csParam.open.localHost;
  286.     sourcePort=                parameters->csParam.open.localPort;
  287.     
  288.     return itsLastError;
  289. End
  290.  
  291. /*===---------------===*/
  292.  
  293. OSErr CTCP::ActiveOpen(void)
  294. Begin
  295.     TCPiopb            pb;                                                        //    Our parameters
  296.     TCPiopbPtr    parameters= &pb;
  297.     
  298.     GetParameters((ParmBlkPtr*)¶meters);    
  299.     
  300.     parameters->csCode=        TCPActiveOpen;
  301.     parameters->ioResult=    kInProgress;
  302.     
  303.     parameters->csParam.open.ulpTimeoutValue=         timeOutDelay;
  304.     parameters->csParam.open.ulpTimeoutAction=        kDefaultAction;
  305.     parameters->csParam.open.validityFlags=                kDefaultFlags;
  306.     parameters->csParam.open.commandTimeoutValue= timeOutDelay;
  307.     parameters->csParam.open.remoteHost=                     remoteAddress;
  308.     parameters->csParam.open.remotePort=                    destinationPort;
  309.     parameters->csParam.open.localPort=                     sourcePort;
  310.     parameters->csParam.open.tosFlags=                        Null;
  311.     parameters->csParam.open.precedence=                     Null;
  312.     parameters->csParam.open.dontFrag=                         False;
  313.     parameters->csParam.open.timeToLive=                     Null;
  314.     parameters->csParam.open.security=                         Null;
  315.     parameters->csParam.open.optionCnt=                     Null;
  316.     
  317.     SetParameters((ParmBlkPtr)parameters);
  318.     SendControlInfoToDriver();                                    //    Set our action...
  319.     if (!fAsynchronous)
  320.         Process();                                                                //    Let it run
  321.         
  322.     GetParameters((ParmBlkPtr*)¶meters);
  323.     localAddress=            parameters->csParam.open.localHost;
  324.     sourcePort=                parameters->csParam.open.localPort;
  325.     
  326.     return itsLastError;
  327. End
  328.  
  329. /*===---------------===*/
  330.  
  331. OSErr CTCP::Send(void)
  332. Begin
  333.     TCPiopb                        pb;                                                //    Our parameters
  334.     TCPiopbPtr                parameters= &pb;
  335.     struct wdsEntry        myWDS[2];                                    //    Global write data structure
  336.  
  337.     GetParameters((ParmBlkPtr*)¶meters);
  338.     
  339.     myWDS[0].length=    outDataLength;                        //    Data to send over the link
  340.     myWDS[0].ptr=         outBuffer;
  341.     myWDS[1].length=    Null;
  342.     myWDS[1].ptr=         Null;
  343.  
  344.     parameters->csCode=     TCPSend;
  345.     parameters->ioResult= kInProgress;
  346.     
  347.     parameters->csParam.send.ulpTimeoutValue=        timeOutDelay;
  348.     parameters->csParam.send.ulpTimeoutAction=    kDefaultAction;
  349.     parameters->csParam.send.validityFlags=            kDefaultFlags;
  350.     parameters->csParam.send.pushFlag=                     False;
  351.     parameters->csParam.send.urgentFlag=                 False;
  352.     parameters->csParam.send.wdsPtr=                         (Ptr)myWDS;
  353.     
  354.     SetParameters((ParmBlkPtr)parameters);
  355.     SendControlInfoToDriver();                                //    Set our action...
  356.     if (!fAsynchronous)
  357.         Process();                                                                //    Let it run
  358.         
  359.     return itsLastError;
  360. End
  361.  
  362. /*===---------------===*/
  363.  
  364. OSErr CTCP::NoCopyRcv(Ptr rdsPtr, short rdsLength)
  365. Begin
  366.     TCPiopb                        pb;                                                //    Our parameters
  367.     TCPiopbPtr                parameters= &pb;
  368.     
  369.     GetParameters((ParmBlkPtr*)¶meters);
  370.     
  371.     parameters->csCode=        TCPNoCopyRcv;
  372.     parameters->ioResult= kInProgress;
  373.     
  374.     parameters->csParam.receive.commandTimeoutValue=    timeOutDelay;
  375.     parameters->csParam.receive.rdsPtr=                                rdsPtr;
  376.     parameters->csParam.receive.rdsLength=                        rdsLength;
  377.     
  378.     SetParameters((ParmBlkPtr)parameters);
  379.     SendControlInfoToDriver();                                //    Set our action...
  380.     if (!fAsynchronous)
  381.         Process();                                                                //    Let it run
  382.         
  383.     GetParameters((ParmBlkPtr*)¶meters);
  384.     fUrgent=    parameters->csParam.receive.urgentFlag;
  385.     fMark=         parameters->csParam.receive.markFlag;
  386.     
  387.     return itsLastError;
  388. End
  389.  
  390. /*===---------------===*/
  391.  
  392. OSErr CTCP::BfrReturn(Ptr rdsPtr)
  393. Begin
  394.     TCPiopb            pb;                                                        //    Our parameters
  395.     TCPiopbPtr    parameters= &pb;
  396.     
  397.     GetParameters((ParmBlkPtr*)¶meters);
  398.     
  399.     parameters->csCode=        TCPRcvBfrReturn;
  400.     parameters->ioResult=    kInProgress;
  401.     
  402.     parameters->csParam.receive.rdsPtr= rdsPtr;
  403.     
  404.     SetParameters((ParmBlkPtr)parameters);
  405.     SendControlInfoToDriver();                                //    Set our action...
  406.     Process();                                                                //    Let it run
  407.     
  408.     return itsLastError;
  409. End
  410.  
  411. /*===---------------===*/
  412.  
  413. OSErr CTCP::Rcv(void)
  414. Begin
  415.     TCPiopb            pb;                                                        //    Our parameters
  416.     TCPiopbPtr    parameters= &pb;
  417.     
  418.     GetParameters((ParmBlkPtr*)¶meters);
  419.     
  420.     parameters->csCode=     TCPRcv;
  421.     parameters->ioResult= kInProgress;
  422.     
  423.     parameters->csParam.receive.commandTimeoutValue=    timeOutDelay;
  424.     parameters->csParam.receive.rcvBuff=                             inBuffer;
  425.     parameters->csParam.receive.rcvBuffLen=                        inDataLength;
  426.     
  427.     SetParameters((ParmBlkPtr)parameters);
  428.     SendControlInfoToDriver();                                //    Set our action...
  429.     if (!fAsynchronous)
  430.         Process();                                                            //    Let it run
  431.         
  432.     GetParameters((ParmBlkPtr*)¶meters);
  433.     inDataLength= parameters->csParam.receive.rcvBuffLen;
  434.     
  435.     return itsLastError;
  436. End
  437.  
  438. /*===---------------===*/
  439.  
  440. OSErr CTCP::Close(void)
  441. Begin
  442.     TCPiopb            pb;                                                        //    Our parameters
  443.     TCPiopbPtr    parameters= &pb;
  444.     
  445.     GetParameters((ParmBlkPtr*)¶meters);
  446.     
  447.     parameters->csCode=     TCPClose;
  448.     parameters->ioResult= kInProgress;
  449.     
  450.     parameters->csParam.close.ulpTimeoutValue=    timeOutDelay;
  451.     parameters->csParam.close.validityFlags=         kDefaultFlags;
  452.     parameters->csParam.close.ulpTimeoutAction= kDefaultAction;
  453.     
  454.     SetParameters((ParmBlkPtr)parameters);
  455.     SendControlInfoToDriver();                                //    Set our action...
  456.     Process();                                                                //    Let it run
  457.     
  458.     return itsLastError;
  459. End
  460.  
  461. /*===---------------===*/
  462.     
  463. OSErr CTCP::Abort(void)
  464. Begin
  465.     TCPiopb            pb;                                                        //    Our parameters
  466.     TCPiopbPtr    parameters= &pb;
  467.     
  468.     GetParameters((ParmBlkPtr*)¶meters);
  469.     
  470.     parameters->csCode=        TCPAbort;
  471.     parameters->ioResult= kInProgress;
  472.     
  473.     SetParameters((ParmBlkPtr)parameters);
  474.     SendControlInfoToDriver();                                //    Set our action...
  475.     Process();                                                                //    Let it run
  476.     
  477.     return itsLastError;
  478. End
  479.  
  480. /*===---------------===*/
  481.     
  482. OSErr CTCP::Release(void)
  483. Begin
  484.     TCPiopb            pb;                                                        //    Our parameters
  485.     TCPiopbPtr    parameters= &pb;
  486.     
  487.     GetParameters((ParmBlkPtr*)¶meters);
  488.     
  489.     parameters->csCode=     TCPRelease;
  490.     parameters->ioResult= kInProgress;
  491.     
  492.     SetParameters((ParmBlkPtr)parameters);
  493.     SendControlInfoToDriver();                                //    Set our action...
  494.     Process();                                                                //    Let it run
  495.     
  496.     GetParameters((ParmBlkPtr*)¶meters);
  497.     streamBuffer= parameters->csParam.create.rcvBuff;
  498.     streamLength= parameters->csParam.create.rcvBuffLen;
  499.     
  500.     if (itsLastError == noErr)
  501.         ReleaseBuffers();
  502.     
  503.     return itsLastError;
  504. End
  505.  
  506. /*===---------------===*/
  507.     
  508. OSErr    CTCP::Status(TCPStatusPB* theStatus)
  509. Begin
  510.     TCPiopb            pb;                                                        //    Our parameters
  511.     TCPiopbPtr    parameters= &pb;
  512.     
  513.     GetParameters((ParmBlkPtr*)¶meters);
  514.     
  515.     parameters->csCode=        TCPStatus;
  516.     parameters->ioResult= kInProgress;
  517.     
  518.     SetParameters((ParmBlkPtr)parameters);
  519.     SendControlInfoToDriver();                                //    Set our action...
  520.     Process();                                                                //    Let it run
  521.         
  522.     GetParameters((ParmBlkPtr*)¶meters);
  523.     BlockMove((Ptr)&(parameters->csParam.status), (Ptr)theStatus, sizeof(TCPStatusPB));
  524.     
  525.     return itsLastError;
  526. End
  527.  
  528. /*===---------------===*/
  529.  
  530. OSErr    CTCP::GlobalInfo(TCPParam* tcpParam, TCPStats* tcpStat)
  531. Begin
  532.     TCPiopb                pb;                                                    //    Our parameters
  533.     TCPiopbPtr        parameters= &pb;
  534.     
  535.     GetParameters((ParmBlkPtr*)¶meters);
  536.     
  537.     parameters->csCode=     TCPGlobalInfo;
  538.     parameters->ioResult= kInProgress;
  539.     
  540.     SetParameters((ParmBlkPtr)parameters);
  541.     SendControlInfoToDriver();                                //    Set our action...
  542.     Process();                                                                //    Let it run
  543.     
  544.     GetParameters((ParmBlkPtr*)¶meters);
  545.     BlockMove(parameters->csParam.globalInfo.tcpStatsPtr, tcpStat, sizeof(TCPStats));
  546.     BlockMove(parameters->csParam.globalInfo.tcpParamPtr, tcpParam, sizeof(TCPParam));
  547.     
  548.     return itsLastError;
  549. End
  550.  
  551. /*===---------------===*/
  552.  
  553. ip_addr    CTCP::GetMyIPAddress(void)
  554. Begin
  555.     if (!localAddress)
  556.     {                                                                                    //    Fetch our address
  557.         TCPiopb                pb, pbStorage;                        //    Our MacTCP parameters
  558.         TCPiopbPtr        parameters= &pbStorage;
  559.         
  560.     
  561.         GetParameters((ParmBlkPtr*)¶meters);//    For storage
  562.         
  563.         parameters= &pb;                                                //    Working parameters
  564.         GetParameters((ParmBlkPtr*)¶meters);//    Get another copy for actual use
  565.         parameters->csCode=     ipctlGetAddr;
  566.         parameters->ioResult= kInProgress;
  567.         
  568.         SetParameters((ParmBlkPtr)parameters);
  569.         SendControlInfoToDriver();                            //    Set our action...
  570.         Process();                                                            //    Let it run
  571.         
  572.         GetParameters((ParmBlkPtr*)¶meters);
  573.         localAddress= ((IPParamBlockPtr)parameters)->ourAddress;
  574.         
  575.         SetParameters((ParmBlkPtr)&pbStorage);    //    Blast the original block back to resotore the streamPtr
  576.     }
  577.     
  578.     return localAddress;
  579. End
  580.  
  581. /*===---------------===*/
  582.  
  583. void CTCP::SetSizeOfParameters(void)
  584. Begin
  585.     this->sizeOfParameters= sizeof(TCPiopb);
  586. End
  587.  
  588. /*===---------------===*/
  589. /*=====================*/